package com.mediatek.factorymode.flashlight;

import com.mediatek.factorymode.R;
import com.mediatek.factorymode.Utils;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import android.hardware.Camera.Parameters;
import android.hardware.Camera;

public class FlashLight extends Activity implements OnClickListener {
	private Button btnOpen, btnClose;
	private Button btnOk, btnFail;
	private SharedPreferences mSp;


	
	private Camera camera = null;  
       private Parameters parameters = null;  

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.flashlight);
		btnOpen = (Button) findViewById(R.id.btn_open);
		btnClose = (Button) findViewById(R.id.btn_close);

		btnOpen.setOnClickListener(this);
		btnClose.setOnClickListener(this);

		btnOk = (Button) findViewById(R.id.bt_ok);
		btnFail = (Button) findViewById(R.id.bt_failed);
		btnOk.setOnClickListener(this);
		btnFail.setOnClickListener(this);
		
		mSp = getSharedPreferences("FactoryMode", 0);
		  camera = Camera.open();   
	//	FlashLightControl.openDevice();
	}

	@Override
	protected void onDestroy() {
	//	FlashLightControl.closeDevice();
		super.onDestroy();
	}
 
	

	
	@Override
	public void onClick(View v) {
		SharedPreferences localSharedPreferences = this.mSp;
		switch (v.getId()) {
		case R.id.btn_open:
			//FlashLightControl.setFlashLight(true);
			if(camera ==null)
			{
				camera = Camera.open();   
			}
                    parameters = camera.getParameters();  
                    parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
                    camera.setParameters(parameters);  
			break;
		case R.id.btn_close:
			//FlashLightControl.setFlashLight(false);
			if(camera ==null)
			{
				camera = Camera.open();   
			}
			parameters = camera.getParameters();  
			parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
                    camera.setParameters(parameters);  
			break;
		case R.id.bt_ok:
			Utils.SetPreferences(this, localSharedPreferences,
					R.string.flashlight, "success");
			camera.release();
			finish();
			break;
		case R.id.bt_failed:
			Utils.SetPreferences(this, localSharedPreferences,
					R.string.flashlight, "failed");
			camera.release();
			finish();
			break;
		default:
			break;
		}
	}

}
